//
//  Sign.swift
//  甜言蜜语
//
//  Created by yingli_yanfa_liuxingyu on 16/9/7.
//  Copyright © 2016年 wl. All rights reserved.
//

import UIKit
protocol signDelegate {
    func signBtnClicked(sender: UIButton)
}
class Sign: UIView {
    
    var delegate: signDelegate?
    var model: signModel?{
        didSet{
            self.setUpView()
        }
    }
    var weekArr: [String]! = []
    @IBOutlet var customView: UIView!
    @IBOutlet weak var MondayBtn: UIButton!
    @IBOutlet weak var TuesdayBtn: UIButton!
    @IBOutlet weak var WednesdayBtn: UIButton!
    @IBOutlet weak var ThursdayBtn: UIButton!
    @IBOutlet weak var FridayBtn: UIButton!
    @IBOutlet weak var Saturday: UIButton!
    @IBOutlet weak var SundayBtn: UIButton!
    
    @IBOutlet weak var signBtn: UIButton!
    
    var maxHeight: CGFloat = 0
     func drawingView() {
        self.MondayBtn.layer.masksToBounds = true
        self.MondayBtn.layer.borderWidth = 1
        self.MondayBtn.layer.cornerRadius = 15
        
        self.TuesdayBtn.layer.masksToBounds = true
        self.TuesdayBtn.layer.borderWidth = 1
        self.TuesdayBtn.layer.cornerRadius = 15
        
        self.WednesdayBtn.layer.masksToBounds = true
        self.WednesdayBtn.layer.borderWidth = 1
        self.WednesdayBtn.layer.cornerRadius = 15
        
        self.ThursdayBtn.layer.masksToBounds = true
        self.ThursdayBtn.layer.borderWidth = 1
        self.ThursdayBtn.layer.cornerRadius = 15
        
        self.FridayBtn.layer.masksToBounds = true
        self.FridayBtn.layer.borderWidth = 1
        self.FridayBtn.layer.cornerRadius = 15
        
        self.Saturday.layer.masksToBounds = true
        self.Saturday.layer.borderWidth = 1
        self.Saturday.layer.cornerRadius = 15
        
        self.SundayBtn.layer.masksToBounds = true
        self.SundayBtn.layer.borderWidth = 1
        self.SundayBtn.layer.cornerRadius = 15
        
       
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.commonInit()
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        self.commonInit()
    }
    
    
    func commonInit () {
        NSBundle.mainBundle().loadNibNamed("Sign", owner: self, options: nil)
        self.customView.frame.size.width = self.frame.size.width
        self.customView.width(self.width)
        self.customView.height(320)
        self.customView.bottom(self.bottom())
        self.backgroundColor = UIColor.clearColor()
        self.alpha = 0
        
        
        let maskView:UIView = UIView.init(frame: self.bounds)
        maskView.backgroundColor = UIColor.blackColor()
        maskView.alpha = 0.5
        self.addSubview(maskView)
        
        self.addSubview(self.customView)
        let tap = UITapGestureRecognizer.init(target: self, action:#selector(Sign.tapClick(_:)))
        maskView.addGestureRecognizer(tap)
        self.viewFrame()
        
    }
    
    func tapClick (sender:UITapGestureRecognizer) {
        self.closeBtnClick()
    }
    
    //MARK:view的位置
    func viewFrame() {
        
        if maxHeight == kScreen_height {
            UIView.animateWithDuration(0.25, animations: { () -> Void in
                self.customView.frame.origin.y = kScreen_height
                self.alpha = 0
            })
        } else {
            UIView.animateWithDuration(0.25, animations: { () -> Void in
                self.alpha = 1
                self.customView.bottom(320)
            })
        }
    }

    
    func closeBtnClick(){
        maxHeight = kScreen_height
        self.viewFrame()
        
    }
    @IBAction func signClicked(sender: UIButton){
        sender.selected = !sender.selected
        if sender.selected{
            delegate?.signBtnClicked(sender)
        }else{
            
        }
        
    }
    
    func setUpView(){
        if self.model?.isqiandao == true{
            self.signBtn.layer.masksToBounds = true
            self.signBtn.layer.borderWidth = 1
            self.signBtn.layer.cornerRadius = 15
            //self.signBtn.backgroundColor = UIColor.lightGrayColor()
            
            self.signBtn.userInteractionEnabled = false
            self.signBtn.setTitle("今日已签到", forState: UIControlState.Normal)
            self.signBtn.setTitleColor(UIColor.lightGrayColor(), forState: UIControlState.Normal)
            
        }else{
            self.signBtn.layer.masksToBounds = true
            self.signBtn.layer.borderWidth = 1
            self.signBtn.layer.cornerRadius = 15
            //self.signBtn.backgroundColor = UIColor.yellowColor()
            self.signBtn.titleLabel?.textColor = NAVIGATIONBAR_RED_COLOR
            self.signBtn.userInteractionEnabled = true

            
        }
        if self.model?.qiandaolist?.count == 0{
            self.drawingView()
        }else{
            for index in 0...(self.model?.qiandaolist?.count)! - 1{
                let weekStr = self.model?.qiandaolist![index].week
                if weekStr == "一"{
                    self.MondayBtn.layer.masksToBounds = true
                    self.MondayBtn.layer.borderWidth = 1
                    self.MondayBtn.layer.cornerRadius = 15
                    self.MondayBtn.layer.borderColor = NAVIGATIONBAR_RED_COLOR.CGColor
                     self.MondayBtn.setTitle("＋1", forState: UIControlState.Normal)
                    self.MondayBtn.titleLabel?.textColor = NAVIGATIONBAR_RED_COLOR
                    self.MondayBtn.backgroundColor = UIColor.yellowColor()
                }else{
                    self.MondayBtn.layer.masksToBounds = true
                    self.MondayBtn.layer.borderWidth = 1
                    self.MondayBtn.layer.cornerRadius = 15
                }
                if weekStr == "二"{
                    self.TuesdayBtn.layer.masksToBounds = true
                    self.TuesdayBtn.layer.borderWidth = 1
                    self.TuesdayBtn.layer.cornerRadius = 15
                    self.TuesdayBtn.layer.borderColor = NAVIGATIONBAR_RED_COLOR.CGColor
                    self.TuesdayBtn.setTitle("＋1", forState: UIControlState.Normal)
                    self.TuesdayBtn.titleLabel?.textColor = NAVIGATIONBAR_RED_COLOR
                    self.TuesdayBtn.backgroundColor = UIColor.yellowColor()
                }else{
                    self.TuesdayBtn.layer.masksToBounds = true
                    self.TuesdayBtn.layer.borderWidth = 1
                    self.TuesdayBtn.layer.cornerRadius = 15
                }
                if weekStr == "三"{
                    self.WednesdayBtn.layer.masksToBounds = true
                    self.WednesdayBtn.layer.borderWidth = 1
                    self.WednesdayBtn.layer.cornerRadius = 15
                    self.WednesdayBtn.layer.borderColor = NAVIGATIONBAR_RED_COLOR.CGColor
                    self.WednesdayBtn.backgroundColor = UIColor.yellowColor()
                    self.WednesdayBtn.setTitle("＋1", forState: UIControlState.Normal)
                    self.WednesdayBtn.titleLabel?.textColor = NAVIGATIONBAR_RED_COLOR
                    
                }else{
                    self.WednesdayBtn.layer.masksToBounds = true
                    self.WednesdayBtn.layer.borderWidth = 1
                    self.WednesdayBtn.layer.cornerRadius = 15
                }
                if weekStr == "四"{
                    self.ThursdayBtn.layer.masksToBounds = true
                    self.ThursdayBtn.layer.borderWidth = 1
                    self.ThursdayBtn.layer.cornerRadius = 15
                    self.ThursdayBtn.layer.borderColor = NAVIGATIONBAR_RED_COLOR.CGColor
                    self.ThursdayBtn.setTitle("＋1", forState: UIControlState.Normal)
                    self.ThursdayBtn.titleLabel?.textColor = NAVIGATIONBAR_RED_COLOR
                    self.ThursdayBtn.backgroundColor = UIColor.yellowColor()
                }else{
                    self.ThursdayBtn.layer.masksToBounds = true
                    self.ThursdayBtn.layer.borderWidth = 1
                    self.ThursdayBtn.layer.cornerRadius = 15
                }
                if weekStr == "五"{
                    self.FridayBtn.layer.masksToBounds = true
                    self.FridayBtn.layer.borderWidth = 1
                    self.FridayBtn.layer.cornerRadius = 15
                    self.FridayBtn.layer.borderColor = NAVIGATIONBAR_RED_COLOR.CGColor
                    self.FridayBtn.setTitle("＋1", forState: UIControlState.Normal)
                    self.FridayBtn.titleLabel?.textColor = NAVIGATIONBAR_RED_COLOR
                    self.FridayBtn.backgroundColor = UIColor.yellowColor()
                }else{
                    self.FridayBtn.layer.masksToBounds = true
                    self.FridayBtn.layer.borderWidth = 1
                    self.FridayBtn.layer.cornerRadius = 15
                }
                if weekStr == "六"{
                    self.Saturday.layer.masksToBounds = true
                    self.Saturday.layer.borderWidth = 1
                    self.Saturday.layer.cornerRadius = 15
                    self.Saturday.layer.borderColor = NAVIGATIONBAR_RED_COLOR.CGColor
                     self.Saturday.setTitle("＋1", forState: UIControlState.Normal)
                    self.Saturday.titleLabel?.textColor = NAVIGATIONBAR_RED_COLOR
                    self.Saturday.backgroundColor = UIColor.yellowColor()
                }else{
                    self.Saturday.layer.masksToBounds = true
                    self.Saturday.layer.borderWidth = 1
                    self.Saturday.layer.cornerRadius = 15
                }
                if weekStr == "七"{
                    self.SundayBtn.layer.masksToBounds = true
                    self.SundayBtn.layer.borderWidth = 1
                    self.SundayBtn.layer.cornerRadius = 15
                    self.SundayBtn.layer.borderColor = NAVIGATIONBAR_RED_COLOR.CGColor
                     self.SundayBtn.setTitle("＋1", forState: UIControlState.Normal)
                    self.SundayBtn.titleLabel?.textColor = NAVIGATIONBAR_RED_COLOR
                    self.SundayBtn.backgroundColor = UIColor.yellowColor()
                }else{
                    self.SundayBtn.layer.masksToBounds = true
                    self.SundayBtn.layer.borderWidth = 1
                    self.SundayBtn.layer.cornerRadius = 15
                }
            }

            
        }

        
    }

    
    

}
